package com.woniuxy.lab.woniu_lab.util;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author 17292
 */
public class WebUtil {


	public static final String X_FORWARDED_FOR = "x-forwarded-for";
	public static final String UNKNOWN = "unknown";
	public static final String LOCALHOST = "127.0.0.1";
	public static final String USER_AGENT = "User-Agent";
	public static final String PROXY_CLIENT_IP = "Proxy-Client-IP";
	public static final String WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";

	/**
     * 获得请求对象
	 * @return HttpServletRequest
	 */
	public static HttpServletRequest getRequest() {
		ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		assert sra != null;
		return sra.getRequest();
	}

    /**
     * 获得响应对象
	 * @return HttpServletResponse
	 */
	public static HttpServletResponse getResponse() {
		ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		assert sra != null;
		return sra.getResponse();
	}


	/**
	 * 获取发起请求的浏览器名称
	 */

	public static String getIp() {
		HttpServletRequest request = getRequest();
		String ip = null;
		ip = request.getHeader(X_FORWARDED_FOR);
		if ((ip == null) || (ip.length() == 0) || (UNKNOWN.equalsIgnoreCase(ip))) {

			ip = request.getHeader(PROXY_CLIENT_IP);
		}
		if ((ip == null) || (ip.length() == 0) || (UNKNOWN.equalsIgnoreCase(ip))) {
			ip = request.getHeader(WL_PROXY_CLIENT_IP);
		}
		if ((ip == null) || (ip.length() == 0) || (UNKNOWN.equalsIgnoreCase(ip))) {
			ip = request.getRemoteAddr();
			if (ip.equals(LOCALHOST)) {
				InetAddress inet = null;
				try {
					inet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				ip = inet.getHostAddress();
			}
		}

		if ((ip != null) && (ip.length() > 15)) {
			if (ip.indexOf(",") > 0) {
				ip = ip.substring(0, ip.indexOf(","));
			}
		}
		return ip;
	}


	/**

	 * 获取发起请求的浏览器版本号

	 */

	public static String getBrowserVersion() {
		HttpServletRequest request = getRequest();
		String header = request.getHeader(USER_AGENT);
		UserAgent userAgent = UserAgent.parseUserAgentString(header);
		//获取浏览器信息
		Browser browser = userAgent.getBrowser();
		//获取浏览器版本号
		Version version = browser.getVersion(header);
		System.out.println(browser.getBrowserType());
		return browser.getName();

	}

	/**

	 * 获取发起请求的操作系统名称

	 */

	public static String getOsName() {
		HttpServletRequest request = getRequest();
		String header = request.getHeader(USER_AGENT);
		UserAgent userAgent = UserAgent.parseUserAgentString(header);
		//获取操作系统信息
		OperatingSystem operatingSystem = userAgent.getOperatingSystem();
		return operatingSystem.getName();
	}

}





